<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchConfig"
        ref="eleTable"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :column="column"
        :getList="getList"
        uniqueId="wmsInTaskScanTable"
        :initForm="otherParams"
      >
      </ele-page>
    </div>
    <ele-form-drawer
      class="dialog-pack"
      :isLoading="isLoading"
      v-model="formDataDialog"
      v-bind="formDescDialog"
      :request-fn="handleSubmit"
      :visible.sync="visible"
      :title="title"
    >
    </ele-form-drawer>
  </div>
</template>

<script>
import {
  operateList,
  searchConfig,
  importFormDescDialog,
  formDescDialog,
  rowConfig,
} from "./config";
import {
  getList,
  rowCreate,
  rowUpdate,
  rowDel,
  rowDetail,
  importData,
  getSnControl,
  getPushStatus,
  getCurrentWarehouseCode,
  getAccountingFlag,
  getAreaCodeList
} from "@/api/inbound/wmsInTaskScan";
import uploadNoAuto from "@/components/upload-no-auto";
export default {
  name: "wmsInTaskScan",
  components: {
    uploadNoAuto,
  },
  data() {
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: {},
      formDescDialog, //新增修改框
      searchConfig,
      operateList,
      getList,
      isLoadingImport: false, // 导入数据loading
      importData: {}, //导入数据
      importVisible: false,
      importFormDescDialog, // 导入字段配置
      otherParams: this.$route.query,
      // 列表页字段的绑定配置
      column: [
        {
          type: "selection",
        },
        {
          type: "index",
          label: $i18n.t("common.order"), // 序号
          fixed: "left",
          width: 60,
        },
        {
          prop: "orderNo",
          label: "入库单据编号", // 单据编号
          width: 160,
          fixed: "left",
          showOverflowTooltip: true,
        },
        {
          prop: "warehouseCode",
          columnKey: "warehouseName",
          label: $i18n.t("common.warehouseName"),
          showOverflowTooltip: true,
          width: 150,
          optionProp: {
            value: "modelingCode",
            label: "modelingName",
          },
          option: async (data) => {
            const list = await getCurrentWarehouseCode();
            return list.data.result;
          },
        },
        {
          prop: "goodsCode",
          label: "商品编码",
          width: 150,
        },
        {
          prop: "goodsName",
          label: "商品名称",
          width: 130,
        },
        {
          prop: "accountingAmount",
          label: "入库数量",
          width: 130,
        },
        {
          prop: "countUnitShow",
          label: "入库单位",
          width: 80,
        },
        {
          prop: "supplierName",
          label: "供应商名称", // 单据行项目
          width: 100,
          showOverflowTooltip: true,
        },
        // {
        //   prop: "palletCode",
        //   label: $i18n.t("wmsInTaskScan.palletCode"), // 托盘码
        // },
        {
          prop: "brand",
          label: "品牌", // 产品唯一码
          width: 100,
          showOverflowTooltip: true,
        },
        // {
        //   prop: "printCode",
        //   label: $i18n.t("wmsInTaskScan.printCode"), // 打印码
        // },
        {
          prop: "orderCode",
          label: "订单号",
          showOverflowTooltip: true,
          width: 140,
        },
        {
          prop: "modelName",
          label: "车型",
          showOverflowTooltip: true,
          width: 160,
        },
        {
          prop: "vinCode",
          label: "车架号", // 规格
          width: 160,
        },
        {
          prop: "createByName",
          label: $i18n.t("common.createByName"), // 创建人名称
          width: 130,
        },
        {
          prop: "createTime",
          label: $i18n.t("common.createTime"), // 创建时间
          width: 160,
        },
      ],
    };
  },
  computed: {},
  methods: {
    // 表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name;
      const fnc = {
        add: () => {
          this.disableFields(false);
          this.formDataDialog = {};
          this.visible = true;
        },
        deleteBatch: () => {
          this.$confirm(
            $i18n.t("common.deleteBatch", { name: $i18n.t("wmsInTaskScan.name") }),
            $i18n.t("common.tip"),
            {
              confirmButtonText: $i18n.t("common.submit"),
              cancelButtonText: $i18n.t("common.cancel"),
              type: "warning",
            }
          ).then(() => {
            const ids = data.rows.map((val) => val.id).join(",");
            this.rowDel(ids);
          });
        },
        import: () => {
          this.importDialogData = {};
          this.importVisible = true;
        },
      };
      fnc[data.handleName]();
    },
    //表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name;
      const fnc = {
        edit: () => {
          this.disableFields(false);
          this.rowDetail(row.id);
          this.visible = true;
        },
        detail: () => {
          this.disableFields(true);
          this.rowDetail(row.id);
          this.visible = true;
        },
        delete: () => {
          this.$confirm(
            $i18n.t("common.deleteSingle", { name: $i18n.t("wmsInTaskScan.name") }),
            $i18n.t("common.tip"),
            {
              confirmButtonText: $i18n.t("common.ok"),
              cancelButtonText: $i18n.t("common.cancel"),
              type: "warning",
            }
          ).then(() => {
            this.rowDel(row.id);
          });
        },
      };
      fnc[handleName]();
    },
    doSubmit({ title, api, data }) {
      this.isLoading = true;
      api(data)
        .then((res) => {
          const message = res.data.message;
          this.$message.success(message || title);
          this.visible = false;
          this.refresh();
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    //弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return; // 判断状态
      const params = this.formDataDialog.id
        ? { title: $i18n.t("common.deleteSuccess"), api: rowUpdate, data }
        : { title: $i18n.t("common.addSuccess"), api: rowCreate, data };
      this.doSubmit(params);
    },
    //删除
    rowDel(data) {
      this.loading = true;
      rowDel(data)
        .then((res) => {
          const message = res.data.message;
          this.$message.success(message || $i18n.t("common.deleteSuccess"));
          this.refresh();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    disableFields(flag) {
      this.formDescDialog.disabled = flag;
      this.formDescDialog.isShowSubmitBtn = !flag;
    },
    rowDetail(id) {
      rowDetail(id).then((res) => {
        this.formDataDialog = res.data.result;
      });
    },
    //刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset);
    },
    //数据文件上传
    upLoadHandleChange(file) {
      this.$set(this.importData, "file", file);
    },
    // 提交导入信息
    handleSubmitImport() {
      if (!this.importData.file) {
        this.$message.error($i18n.t("common.uploadMsg"));
        return;
      }
      const formData = new FormData();
      formData.append("file", this.importData.file.get("file"));
      this.isLoadingImport = true;
      importData(formData)
        .then((res) => {
          this.$message.success($i18n.t("common.importSuccess"));
          this.isLoadingImport = false;
          this.importVisible = false;
          this.refresh();
        })
        .catch((error) => {
          console.log("错误", error);
          this.$set(this.importData, "file", null);
        })
        .finally(() => {
          this.isLoadingImport = false;
          this.$refs.uploadNoAuto.clearFiles();
        });
    },
  },
};
</script>
